#include "SampleTexture.h"
#include "GLUtils.h"
#include "LogUtils.h"

// 正方形/长方形

SampleTexture::SampleTexture() {
    squareVertices = new GLfloat[] {
            -0.5f, 0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f,
            0.5f, 0.5f, 0.0f,
    };
    squareColorVertices = new GLfloat[] {
            1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 1.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f, 1.0f,
            0.0f, 0.0f, 0.0f, 1.0f,
    };
    m_TextureId = 0;
}

SampleTexture::~SampleTexture() {
    delete [] squareVertices;
    delete [] squareColorVertices;
    NativeImageUtil::FreeNativeImage(&m_RenderImage);
}


void SampleTexture::LoadImage(NativeImage *pImage) {
    LOGCATE("SampleTexture::LoadImage pImage = %p", pImage->ppPlane[0]);
    if (pImage)
    {
        LOGCATE("SampleTexture::LoadImage w:%d h:%d", pImage->width, pImage->height);
        m_RenderImage.width = pImage->width;
        m_RenderImage.height = pImage->height;
        m_RenderImage.format = pImage->format;
        NativeImageUtil::CopyNativeImage(pImage, &m_RenderImage);
    }
}

void SampleTexture::Init() {
    //create RGBA texture
    glGenTextures(1, &m_TextureId);
    glBindTexture(GL_TEXTURE_2D, m_TextureId);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glBindTexture(GL_TEXTURE_2D, GL_NONE);

    char vShaderStr[] =
            "#version 300 es                          \n"
            "layout(location = 0) in vec4 vPosition;  \n"
            "layout(location = 1) in vec2 a_texCoord;   \n"
            "out vec2 v_texCoord;                       \n"
            "void main()                              \n"
            "{                                        \n"
            "   gl_Position = vPosition;              \n"
            "   v_texCoord = a_texCoord;                \n"
            "}                                        \n";

    char fShaderStr[] =
            "#version 300 es                              \n"
            "precision mediump float;                     \n"
            "in vec2 v_texCoord;                                 \n"
            "layout(location = 0) out vec4 outColor;             \n"
            "uniform sampler2D s_TextureMap;                     \n"
            "void main()                                  \n"
            "{                                            \n"
            "  outColor = texture(s_TextureMap, v_texCoord);     \n"
            "}                                            \n";

    m_ProgramObj = GLUtils::CreateProgram(vShaderStr, fShaderStr, m_VertexShader, m_FragmentShader);
    if (m_ProgramObj)
    {
        m_SamplerLoc = glGetUniformLocation(m_ProgramObj, "s_TextureMap");
    }
//    glEnable(GL_DEPTH_TEST);
}

void SampleTexture::Draw() {
//    GLushort indices[6] = {0, 1, 2, 0, 2, 3};
    LOG_DEBUG("SampleTexture::Draw\n");

    if (m_ProgramObj == 0 || m_TextureId == GL_NONE)
        return;

    glClear(GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearColor(1.0, 1.0, 1.0, 1.0);

    GLfloat verticesCoords[] = {
            -0.8f,  1.0f, 0.0f,  // Position 0
            -0.8f, -1.0f, 0.0f,  // Position 1
            0.8f, -1.0f, 0.0f,   // Position 2
            0.8f,  1.0f, 0.0f,   // Position 3
    };

    GLfloat textureCoords[] = {
            0.2f,  0.0f,        // TexCoord 0
            0.2f,  0.7f,        // TexCoord 1
            0.8f,  0.7f,        // TexCoord 2
            0.8f,  0.0f         // TexCoord 3
    };

    GLushort indices[] = { 0, 1, 2, 0, 2, 3 };

    //upload RGBA image data
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_TextureId);
    LOG_DEBUG("SampleTexture::Draw w:%d h:%d\n", m_RenderImage.width, m_RenderImage.height);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
            m_RenderImage.width, m_RenderImage.height, 0,
            GL_RGBA, GL_UNSIGNED_BYTE, m_RenderImage.ppPlane[0]);
    glBindTexture(GL_TEXTURE_2D, GL_NONE);

    // Use the program object
    glUseProgram (m_ProgramObj);

    // Load the vertex position
    glVertexAttribPointer (0, 3, GL_FLOAT,
                           GL_FALSE, 3 * sizeof (GLfloat), verticesCoords);
    // Load the texture coordinate
    glVertexAttribPointer (1, 2, GL_FLOAT,
                           GL_FALSE, 2 * sizeof (GLfloat), textureCoords);

    glEnableVertexAttribArray (0);
    glEnableVertexAttribArray (1);

    // Bind the RGBA map
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_TextureId);

    // Set the RGBA map sampler to texture unit to 0
    glUniform1i(m_SamplerLoc, 0);

    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);

//    glDisableVertexAttribArray(0);
//    glDisableVertexAttribArray (1);
}

void SampleTexture::Destroy() {
    if (m_ProgramObj) {
        glDeleteProgram(m_ProgramObj);
        glDeleteTextures(1, &m_TextureId);
        m_ProgramObj = GL_NONE;
    }
}

void SampleTexture::setParentViewSize(int w, int h) {
    m_ParentViewWidth = w;
    m_ParentViewHeight = h;
    m_ratio = (float)w/h;
    GLfloat x = 0.5 * w / h;
    LOG_DEBUG("SampleTexture::setParentViewSize w:%d h:%d x%f \n", w, h, x);

    // 暂时： 通过计算使得长方形变成正方形
    // 后续通过矩阵来实现
//    squareVertices[1] = squareVertices[10]  = x;
//    squareVertices[4] = squareVertices[7] = 0-x;
}
void SampleTexture::OnSurfaceCreated() {

}
void SampleTexture::OnSurfaceChanged(int width, int height) {
    setParentViewSize(width, height);
}